<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */



class SignUpForm extends HTML_QuickForm {
  private $tpl;
  private $user;
  
  function SignUpForm($tpl,$user) {
    $this->tpl = $tpl;
    $this->user = $user;       
    
    $this->setConstants(array('password1' => uniqid(),
                              'password2' => uniqid())); 
     
    parent::HTML_QuickForm('signupForm','post','','','onsubmit="javascript:password1.value=hex_sha1(password1.value), password2.value=hex_sha1(password2.value)"');
    $this->addElement('header', 'header', 'signup'); 
    $this->addElement('text', 'firstname', _('firstname'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('text', 'lastname', _('lastname'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('text', 'email', _('email'), array('size' => 30, 'maxlength' => 100));
    $this->addElement('text', 'login', _('login'), array('size' => 15, 'maxlength' => 20));
    $this->addElement('password', 'password1', _('password'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('password', 'password2', _('repeat password'), array('size' => 30, 'maxlength' => 50));
    $sex[] =& $this->createElement('radio', null, null, _('male'), 'M', array('class' => 'radio'));
    $sex[] =& $this->createElement('radio', null, null, _('female'), 'F', array('class' => 'radio'));
    $this->addGroup($sex, 'sex', _('sex'), '&nbsp;');
    $this->addElement('submit', 'btn_Submit', 'Enviar', array('class' => 'submit','onsubmit' => 'reset()'));
    $this->applyFilter('__ALL__', 'trim');
    $this->registerRule('check_exists_email','function','checkExistsEmail', $this);
    $this->registerRule('check_exists_login','function','checkExistsLogin', $this);
    $this->registerRule('compare', 'function', 'comparePassword', $this);
    $this->addRule('firstname', _('firstname required'), 'required');
    $this->addRule('firstname', _('min lenght 3'), 'minlength', 3);
    $this->addRule('firstname', _('characters not allowed'), 'regex', '/^[^[:punct:][:digit:]]+$/');
    $this->addRule('lastname', _('lastname required'), 'required');
    $this->addRule('lastname', _('min lenght 3'), 'minlength', 3);
    $this->addRule('lastname', _('characters not allowed'), 'regex', '/^[^[:punct:][:digit:]]+$/');
    $this->addRule('email', _('email required'), 'required');
    $this->addRule('email', _('verify your email'), 'email');
    $this->addRule('email', _('email already exists'), 'check_exists_email', $this->user);
    $this->addRule('login', _('username required'), 'required');
    $this->addRule('login', _('min lenght 3'), 'minlength', 3);
    $this->addRule('login', _('characters not allowed'), 'alphanumeric');
    $this->addRule('login', _('username already exists'), 'check_exists_login', $this->user);
    $this->addRule('password1', _('password required'), 'required');
    $this->addRule('password1', _('min lenght 4'), 'minlength', 4);
    $this->addRule('password1', _('passwords do not match'), 'compare');
    $this->addRule('password2', _('password required'), 'required');
    $this->addRule('password2', _('min lenght 4'), 'minlength', 4);
    $this->addRule('sex', _('sex required'), 'required');  
    
  }
  
  function checkExistsEmail() {     
    $this->user->setEmail($this->getSubmitValue('email'));
    return $this->user->checkExists($post='email');
  }
  
  function checkExistsLogin() {
    $this->user->setLogin($this->getSubmitValue('login'));
    return $this->user->checkExists($post='login');
  }
  
  function comparePassword() {
    if ($this->getSubmitValue('password1') == $this->getSubmitValue('password2')) {
      return true;
    } else {
      return false;
    } 
  }
    
  function displayForm() {      
    $this->renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);  
   	                                      
   	$this->accept($this->renderer);           
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->user->getMsg());   
  }
  
  function process() { 
    $this->user->setEmailSubject(_('Sign up confirmation code'));
    $this->user->setEmailBody(_('You have registered the account __login__ on __sitename__, for activating your account follow the next link: __url__'));
    $this->user->createSignup($this->getSubmitValues());
    $this->tpl->assign('result',true);
    $this->tpl->assign('msg',$this->user->getMsg());
  }
   
}

?>